﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace M5.GeminiHybird.Core.BaseForm
{

    public partial class M5FromNormal : Form
    {
        public static class ClassStyles
        {
            public static readonly Int32
            CS_BYTEALIGNCLIENT = 0x1000,
            CS_BYTEALIGNWINDOW = 0x2000,
            CS_CLASSDC = 0x0040,
            CS_DBLCLKS = 0x0008,
            CS_DROPSHADOW = 0x00020000,
            CS_GLOBALCLASS = 0x4000,
            CS_HREDRAW = 0x0002,
            CS_NOCLOSE = 0x0200,
            CS_OWNDC = 0x0020,
            CS_PARENTDC = 0x0080,
            CS_SAVEBITS = 0x0800,
            CS_VREDRAW = 0x0001;
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= ClassStyles.CS_DROPSHADOW;
                return cp;
            }
        }

        public M5WebBrowser _chromeBrowser { get; set; }

        private string _initUrl { get; set; }

        public M5FromNormal()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        public M5FromNormal(string initUrl)
        {
            _initUrl = initUrl;
            InitializeComponent();
        }

        private void M5FromNormalStyle_Load(object sender, EventArgs e)
        {

            if (DesignMode)
                return;

            _chromeBrowser = new M5WebBrowser(_initUrl);
            _chromeBrowser.Dock = DockStyle.Fill;
            _chromeBrowser.Location = new Point(0, 0);
            _chromeBrowser.LoadHandler.OnLoadEnd += LoadHandler_OnLoadEnd;
        }

        private void LoadHandler_OnLoadEnd(object sender, Chromium.Event.CfxOnLoadEndEventArgs e)
        {

            //MessageBox.Show("loadEnd");
            Action action = () =>
            {
                var loadingControls = ContentPanel.Controls.Find("lb_loading", false);
                if (loadingControls.Length > 0)
                    ContentPanel.Controls.Remove(loadingControls[0]);
                ContentPanel.Controls.Add(_chromeBrowser);
            };
            Invoke(action);

        }
    }
}
